# -*- coding: UTF-8 -*-

import xeres
from ui import font

import interface
import convert

# Font properties
class FontApply(interface.sheet_property):
	def __init__(self):
		self.face = None
		self.height = None
		self.weight = None
		self.italic = None
		self.antialias = None
	
	def apply(self,pseudo,styles):
		return font.reset_font(font.font(),self.face,self.height,self.weight,self.italic,self.antialias)

	# style parameter
	def font_all(self,name,attr):
		pass
	
	# set font face
	def font_face(self,name,attr):
		if not xeres.QueryFont(attr):
			print "Font [", attr, "] is not in system!"
			return
		self.face = attr

	# set font face by family
	def font_family(self,name,attr):
		# find fonts
		fonts = attr.split(',')
		found = None
		for i in fonts:
			print "query font " , i
			if xeres.QueryFont(i):
				print "found font",i
				found = i
				break
		if found == None:
			# give a warning
			print "Font list [", attr, "] has no match in system fonts! Use default instead."
			return

		# override
		self.face = found
			
	# font size
	def font_size(self,name,attr):
		# extract size
		try:
			val = float(attr)
		except ValueError:
			print "Invalid value for '%s' property." %name
			return
		self.height = int(val)
	
	# font weight
	def font_weight(self,name,attr):
		# extract size
		try:
			val = float(attr)
		except ValueError:
			print "Invalid value for 'font-weight' attribute."
			return
		self.weight = int(val)

	# font italic
	def font_italic(self,name,attr):
		if attr == 'normal':
			self.italic = False
		elif attr == 'italic':
			self.italic = True
		else:
			print "Invalid value for '%s' property." %name

	# font italic
	def font_italic(self,name,attr):
		if attr == 'normal':
			self.italic = False
		elif attr == 'italic':
			self.italic = True
		else:
			print "Invalid value for '%s' property." %name
	
	# font antialias
	def font_antialias(self,name,attr):
		if attr == 'none':
			self.antialias = False
		elif attr == 'cleartype':
			self.antialias = True
		else:
			print "Invalid value for '%s' property." %name

# text decoration
class DecorationApply(interface.sheet_property):
	MAP = {\
		'none':xeres.TextDecoration.none,\
		'underline':xeres.TextDecoration.underline,\
		'overline':xeres.TextDecoration.overline,\
		'bothline':xeres.TextDecoration.bothline,\
		'line-through':xeres.TextDecoration.line_through,\
	}
	# set decoration
	def set(self,name,attr):
		if not attr in DecorationApply.MAP:
			print "No such type decoration (",attr,") for property '%s'." %name
			return
		self.val = DecorationApply.MAP[attr]

# text transform
class TransformApply(interface.sheet_property):
	MAP = {\
		'normal':xeres.TextTransform.normal,\
		'capitalize':xeres.TextTransform.capitalize,\
		'lowercase':xeres.TextTransform.lowercase,\
		'uppercase':xeres.TextTransform.uppercase,\
	}
	# set transform
	def set(self,name,attr):
		if not attr in TransformApply.MAP:
			print "No such type transform (",attr,") for property '%s'." %name
			return
		self.val = TransformApply.MAP[attr]

# text unit related attribute
class TextUnitApply(interface.sheet_property):
	def apply(self,c1):
		# override my value if any full-picture related change.
		
		# then apply it
		super(TextUnitApply,self).apply(c1)
	
	# set text unit
	def set(self,name,attr):
		pass



